| | | 1 | | // Copyright (c) 2020-2023 dotBunny Inc. |
| | | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace GDX |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// A collection of reflection related helper utilities. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks>Torn about the existence of this utility class, yet alone the conditions dictating it.</remarks> |
| | | 14 | | public static class Reflection |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// <see cref="BindingFlags"/> for a private field. |
| | | 18 | | /// </summary> |
| | | 19 | | public const BindingFlags PrivateFieldFlags = BindingFlags.Instance | BindingFlags.NonPublic; |
| | | 20 | | /// <summary> |
| | | 21 | | /// <see cref="BindingFlags"/> for a private static. |
| | | 22 | | /// </summary> |
| | | 23 | | public const BindingFlags PrivateStaticFlags = BindingFlags.Static | BindingFlags.NonPublic; |
| | | 24 | | /// <summary> |
| | | 25 | | /// <see cref="BindingFlags"/> for a public static. |
| | | 26 | | /// </summary> |
| | | 27 | | public const BindingFlags PublicStaticFlags = BindingFlags.Static | BindingFlags.Public; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns the default value for a given type. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="type">A qualified type.</param> |
| | | 33 | | /// <returns>The default value.</returns> |
| | | 34 | | public static object GetDefault(this Type type) |
| | 2 | 35 | | { |
| | 2 | 36 | | if (type.IsClass || !type.IsValueType) |
| | 1 | 37 | | { |
| | 1 | 38 | | return null; |
| | | 39 | | } |
| | 1 | 40 | | return Activator.CreateInstance(type); |
| | 2 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns a qualified type.. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <param name="type">The full name of a type.</param> |
| | | 47 | | /// <returns>A <see cref="System.Type"/> if found.</returns> |
| | | 48 | | public static Type GetType(string type) |
| | 29 | 49 | | { |
| | 29 | 50 | | Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); |
| | 29 | 51 | | int loadedAssembliesCount = loadedAssemblies.Length; |
| | 5066 | 52 | | for (int i = 0; i < loadedAssembliesCount; i++) |
| | 2531 | 53 | | { |
| | 2531 | 54 | | Type targetType = loadedAssemblies[i].GetType(type); |
| | 2531 | 55 | | if (targetType != null) |
| | 27 | 56 | | { |
| | 27 | 57 | | return targetType; |
| | | 58 | | } |
| | 2504 | 59 | | } |
| | 2 | 60 | | return null; |
| | 29 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Invokes a known static method. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="type">The explicit type of the static class.</param> |
| | | 67 | | /// <param name="method">The name of the method to invoke.</param> |
| | | 68 | | /// <param name="parameters">Any parameters that should be passed to the method?</param> |
| | | 69 | | /// <param name="flags">The <paramref name="method"/>'s access flags.</param> |
| | | 70 | | /// <returns>An <see cref="object"/> of the return value. This can be null.</returns> |
| | | 71 | | public static object InvokeStaticMethod(string type, string method, object[] parameters = null, |
| | | 72 | | BindingFlags flags = PublicStaticFlags) |
| | 6 | 73 | | { |
| | 6 | 74 | | Type targetType = GetType(type); |
| | 6 | 75 | | if (targetType != null) |
| | 5 | 76 | | { |
| | 5 | 77 | | MethodInfo targetMethod = targetType.GetMethod(method, flags); |
| | 5 | 78 | | if (targetMethod != null) |
| | 4 | 79 | | { |
| | 4 | 80 | | return targetMethod.Invoke(null, parameters ?? Core.EmptyObjectArray); |
| | | 81 | | } |
| | 1 | 82 | | } |
| | 2 | 83 | | return null; |
| | 6 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Set the field or property value of a specific <paramref name="targetObject"/>, which may not be |
| | | 88 | | /// normally accessible. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="targetObject">The instanced object which will have it's field or property value set.</param> |
| | | 91 | | /// <param name="name">The field or property's name to set.</param> |
| | | 92 | | /// <param name="value">The value to set the field or property to.</param> |
| | | 93 | | /// <param name="fieldFlags">The field's access flags.</param> |
| | | 94 | | /// <param name="propertyFlags">The property's access flags.</param> |
| | | 95 | | /// <returns>true/false if the value was set.</returns> |
| | | 96 | | public static bool SetFieldOrPropertyValue(object targetObject, string name, object value, |
| | | 97 | | BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags) |
| | 6 | 98 | | { |
| | 6 | 99 | | if (targetObject == null) |
| | 1 | 100 | | return false; |
| | | 101 | | |
| | 5 | 102 | | Type type = targetObject.GetType(); |
| | 5 | 103 | | FieldInfo f = type.GetField(name, fieldFlags); |
| | 5 | 104 | | if (f != null) |
| | 2 | 105 | | { |
| | 2 | 106 | | f.SetValue(targetObject, value); |
| | 2 | 107 | | return true; |
| | | 108 | | } |
| | | 109 | | |
| | 3 | 110 | | PropertyInfo p = type.GetProperty(name,propertyFlags); |
| | 3 | 111 | | if (p != null) |
| | 2 | 112 | | { |
| | 2 | 113 | | p.SetValue(targetObject, value); |
| | 2 | 114 | | return true; |
| | | 115 | | } |
| | | 116 | | |
| | 1 | 117 | | return false; |
| | 6 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Set the field value of a specific <paramref name="targetObject"/>, which may not be normally accessible. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="targetObject">The instanced object which will have it's field value set; use a null value if th |
| | | 124 | | /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param> |
| | | 125 | | /// <param name="name">The field's name to set.</param> |
| | | 126 | | /// <param name="value">The value to set the field to.</param> |
| | | 127 | | /// <param name="flags">The field's access flags.</param> |
| | | 128 | | /// <returns>true/false if the value was set.</returns> |
| | | 129 | | public static bool SetFieldValue(object targetObject, Type type, string name, object value, |
| | | 130 | | BindingFlags flags = PrivateFieldFlags) |
| | 6 | 131 | | { |
| | 6 | 132 | | if (type != null) |
| | 5 | 133 | | { |
| | 5 | 134 | | FieldInfo field = type.GetField(name, flags); |
| | 5 | 135 | | if (field != null) |
| | 4 | 136 | | { |
| | 4 | 137 | | field.SetValue(targetObject, value); |
| | 4 | 138 | | return true; |
| | | 139 | | } |
| | 1 | 140 | | } |
| | 2 | 141 | | return false; |
| | 6 | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Set the property value of a specific <paramref name="targetObject"/>, which may not be normally accessib |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name="targetObject">The instanced object which will have it's property value set; use a null value if |
| | | 148 | | /// <param name="type">The type of the <paramref name="targetObject"/>.</param> |
| | | 149 | | /// <param name="name">The property's name to set.</param> |
| | | 150 | | /// <param name="value">The value to set the property to.</param> |
| | | 151 | | /// <param name="flags">The property's access flags.</param> |
| | | 152 | | /// <returns>true/false if the value was set.</returns> |
| | | 153 | | public static bool SetPropertyValue(object targetObject, Type type, string name, object value, |
| | | 154 | | BindingFlags flags = PrivateFieldFlags) |
| | 4 | 155 | | { |
| | 4 | 156 | | if (type != null) |
| | 3 | 157 | | { |
| | 3 | 158 | | PropertyInfo property = type.GetProperty(name, flags); |
| | 3 | 159 | | if (property != null) |
| | 2 | 160 | | { |
| | 2 | 161 | | property.SetValue(targetObject, value); |
| | 2 | 162 | | return true; |
| | | 163 | | } |
| | 1 | 164 | | } |
| | 2 | 165 | | return false; |
| | 4 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Try to access the field value of a specific <paramref name="targetObject"/>, which may not be normally a |
| | | 170 | | /// </summary> |
| | | 171 | | /// <remarks></remarks> |
| | | 172 | | /// <param name="targetObject">The instanced object which will have it's field value read; use a null value if t |
| | | 173 | | /// <param name="type">The qualified type of the <paramref name="targetObject"/>.</param> |
| | | 174 | | /// <param name="name">The field's name to read.</param> |
| | | 175 | | /// <param name="returnValue">The returned value from the field, the default value if the field was unable to be |
| | | 176 | | /// <param name="flags">The field's access flags.</param> |
| | | 177 | | /// <typeparam name="T">The type of data being read from the field.</typeparam> |
| | | 178 | | /// <returns>true/false if the process was successful.</returns> |
| | | 179 | | public static bool TryGetFieldValue<T>(object targetObject, Type type, string name, out T returnValue, BindingFl |
| | 7 | 180 | | { |
| | 7 | 181 | | if (type == null) |
| | 1 | 182 | | { |
| | 1 | 183 | | returnValue = default; |
| | 1 | 184 | | return false; |
| | | 185 | | } |
| | 6 | 186 | | FieldInfo fieldInfo = type.GetField(name, flags); |
| | 6 | 187 | | if (fieldInfo == null) |
| | 1 | 188 | | { |
| | 1 | 189 | | returnValue = default; |
| | 1 | 190 | | return false; |
| | | 191 | | } |
| | 5 | 192 | | returnValue = (T)fieldInfo.GetValue(targetObject); |
| | 5 | 193 | | return true; |
| | 7 | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Try to access the field or property value of a specific <paramref name="targetObject"/>, which may not |
| | | 198 | | /// be normally accessible. |
| | | 199 | | /// </summary> |
| | | 200 | | /// <remarks>Useful for when you really do not know the <see cref="System.Type"/>.</remarks> |
| | | 201 | | /// <param name="targetObject">The instanced object which will have it's field or property value read.</param> |
| | | 202 | | /// <param name="name">The field or property's name to read.</param> |
| | | 203 | | /// <param name="returnValue">The returned value from the field or property, the default value if the property w |
| | | 204 | | /// <param name="fieldFlags">The field's access flags.</param> |
| | | 205 | | /// <param name="propertyFlags">The property's access flags.</param> |
| | | 206 | | /// <returns>true/false if a value was found.</returns> |
| | | 207 | | public static bool TryGetFieldOrPropertyValue(object targetObject, string name, out object returnValue, |
| | | 208 | | BindingFlags fieldFlags = PrivateFieldFlags, BindingFlags propertyFlags = PrivateFieldFlags) |
| | 7 | 209 | | { |
| | 7 | 210 | | if (targetObject == null) |
| | 1 | 211 | | { |
| | 1 | 212 | | returnValue = null; |
| | 1 | 213 | | return false; |
| | | 214 | | } |
| | | 215 | | |
| | 6 | 216 | | Type type = targetObject.GetType(); |
| | 6 | 217 | | FieldInfo f = type.GetField(name, fieldFlags); |
| | 6 | 218 | | if (f != null) |
| | 2 | 219 | | { |
| | 2 | 220 | | returnValue = f.GetValue(targetObject); |
| | 2 | 221 | | return true; |
| | | 222 | | } |
| | | 223 | | |
| | 4 | 224 | | PropertyInfo p = type.GetProperty(name,propertyFlags); |
| | 4 | 225 | | if (p != null) |
| | 3 | 226 | | { |
| | 3 | 227 | | returnValue = p.GetValue(targetObject); |
| | 3 | 228 | | return true; |
| | | 229 | | } |
| | | 230 | | |
| | 1 | 231 | | returnValue = default; |
| | 1 | 232 | | return false; |
| | 7 | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <summary> |
| | | 236 | | /// Try to get a property value from <paramref name="targetObject"/>, which may not be normally accessible. |
| | | 237 | | /// </summary> |
| | | 238 | | /// <param name="targetObject">The instanced object which will have it's property value read; use a null value i |
| | | 239 | | /// <param name="type">The explicit type of the <paramref name="targetObject"/>.</param> |
| | | 240 | | /// <param name="name">The property's name to read.</param> |
| | | 241 | | /// <param name="returnValue">The returned value from the property, the default value if the property was unable |
| | | 242 | | /// <param name="flags">The property's access flags.</param> |
| | | 243 | | /// <typeparam name="T">The type of data being read from the property.</typeparam> |
| | | 244 | | /// <returns>true/false if the process was successful.</returns> |
| | | 245 | | public static bool TryGetPropertyValue<T>(object targetObject, Type type, string name, out T returnValue, Bindin |
| | 5 | 246 | | { |
| | 5 | 247 | | if (type == null) |
| | 1 | 248 | | { |
| | 1 | 249 | | returnValue = default; |
| | 1 | 250 | | return false; |
| | | 251 | | } |
| | 4 | 252 | | PropertyInfo propertyInfo = type.GetProperty(name, flags); |
| | 4 | 253 | | if (propertyInfo == null) |
| | 1 | 254 | | { |
| | 1 | 255 | | returnValue = default; |
| | 1 | 256 | | return false; |
| | | 257 | | } |
| | | 258 | | |
| | 3 | 259 | | returnValue = (T)propertyInfo.GetValue(targetObject); |
| | 3 | 260 | | return true; |
| | 5 | 261 | | } |
| | | 262 | | } |
| | | 263 | | } |